Skip to content

Commit

Permalink
support sourcemap, Javey/vdt.js#12
Browse files Browse the repository at this point in the history
  • Loading branch information
Javey committed Aug 15, 2018
1 parent c53a770 commit 4c46949
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 69 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
node_modules/
npm-debug.log
.vscode/
61 changes: 45 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
var Vdt = require('vdt'),
loaderUtils = require('loader-utils');
loaderUtils = require('loader-utils'),
path = require('path'),
sourceMap = require('source-map');

// module.exports = function() {};
module.exports = function(source) {
if (this.cacheable) this.cacheable();

Expand All @@ -19,23 +20,51 @@ module.exports = function(source) {
query = Object.assign({
noWith: true,
onlySource: true,
delimiters: ['{', '}']
delimiters: ['{', '}'],
sourceMap: false,
// sourceMap: this.sourceMap
}, query);

var fn = Vdt.compile(source, query);
// source = fn.source;

// var pos = source.indexOf('\n');
var head = fn.head || '';

// 当字符串或者v-raw中存在import语句时,也会被提取,所以先去掉
// fn.source = fn.source.replace(/(import\s+?[^\(\)]*?(from)?['"].*?['"](\s*;)?)/g, function(match) {
// head += match;
// return '';
// });

return [
head,
'export default ' + fn.source
].join('\n');
var content = 'export default ' + fn.source;
if (head) {
content = head + content;
}

if (query.sourceMap) {
var sourceRoot = process.cwd();
var filename = path.relative(sourceRoot, this.resourcePath);
var map = getSourceMap({
mappings: fn.mappings,
source: source,
filename: filename,
sourceRoot: sourceRoot,
offsetLine: head.split(/\n/).length - 1
});

this.callback(null, content, map);
} else {
this.callback(null, content);
}
};

function getSourceMap(options) {
var generator = new sourceMap.SourceMapGenerator({
sourceRoot: options.sourceRoot,
file: path.basename(options.filename)
});

generator.setSourceContent(options.filename, options.source);

options.mappings.forEach(function(mapping) {
mapping.generated.line += options.offsetLine;
if (mapping.original) {
mapping.source = options.filename;
}
generator.addMapping(mapping);
});

return generator.toJSON();
}
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,15 @@
"author": "[email protected]",
"license": "ISC",
"dependencies": {
"loader-utils": "^1.1.0"
"loader-utils": "^1.1.0",
"source-map": "^0.7.3"
},
"peerDependencies": {
"vdt": "*"
},
"devDependencies": {
"babel-core": "^6.26.3",
"babel-loader": "^7.1.5",
"intact": "^2.2.0-17",
"mocha": "^3.4.2",
"webpack": "^2.7.0"
Expand Down
31 changes: 17 additions & 14 deletions test/output/delimiters.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 30 additions & 24 deletions test/output/import.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 21 additions & 14 deletions test/output/skipWhitespace.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ var config = {
path: output,
filename: '[name].js'
},
devtool: '#source-map',
module: {
rules: [
{
Expand All @@ -25,6 +26,15 @@ var config = {
}
};

// config.entry = {
// import: path.resolve(__dirname, './input/import.js')
// };

// webpack(config, function(err, stats) {
// console.log(stats.toString({colors: true}));
// });


describe('Vdt Loader', function() {
it('set error delimiters should get error result', function(done) {
config.module.rules[0].use.options = {delimiters: ['{{', '}}']};
Expand Down

0 comments on commit 4c46949

Please sign in to comment.