Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New [transformFileName] loader-specific option has been implemented. #738

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@babel/cli": "^7.0.0",
"@babel/core": "^7.0.0",
"@babel/preset-env": "^7.0.0",
"@babel/preset-typescript": "^7.1.0",
"ava": "0.25.0",
"babel-eslint": "^8.0.0",
"babel-plugin-istanbul": "^4.0.0",
Expand Down
14 changes: 13 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,18 @@ async function loader(source, inputSourceMap, overrides) {
delete loaderOptions.sourceMap;
}

let transformedFileName = filename;
if (loaderOptions.transformFileName) {
const pattern = loaderOptions.transformFileName.pattern;
const replace = loaderOptions.transformFileName.replace;

if (pattern && replace) {
transformedFileName = filename.replace(pattern, replace);
}
}

const programmaticOptions = Object.assign({}, loaderOptions, {
filename,
filename: transformedFileName,
inputSourceMap: inputSourceMap || undefined,

// Set the default sourcemap behavior based on Webpack's mapping flag,
Expand All @@ -137,7 +147,9 @@ async function loader(source, inputSourceMap, overrides) {
// modules.
sourceFileName: filename,
});

// Remove loader related options
delete programmaticOptions.transformFileName;
delete programmaticOptions.customize;
delete programmaticOptions.cacheDirectory;
delete programmaticOptions.cacheIdentifier;
Expand Down
12 changes: 12 additions & 0 deletions test/fixtures/TestTS.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default class TestTsCls{
private name: string;

constructor(){
this.name = "SomeName";
}

someMethod(arg1: string): number{
return 42;
}

}
12 changes: 12 additions & 0 deletions test/fixtures/TestTsVUE.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
export default class TestTsVueCls{
private name123: string;

constructor(){
this.name123 = "SomeName123";
}

someMethod123(arg1: string): number{
return 42123;
}

}
77 changes: 77 additions & 0 deletions test/loader.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,80 @@ test.cb(
});
},
);

test.cb(
"should perform all *.js, *.ts and *.vue files compilation successfully",
t => {
const config = {
mode: "development",
entry: {
firstEntry: path.join(__dirname, "fixtures/basic.js"),
secondEntry: path.join(__dirname, "fixtures/TestTS.ts"),
thirdEntry: path.join(__dirname, "fixtures/TestTsVUE.vue"),
},
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.((jsx?)|ts|vue)$/,
loader: babelLoader,
options: {
presets: ["@babel/preset-env", "@babel/preset-typescript"],
transformFileName: {
pattern: /(\.vue)$/,
replace: "$1.ts",
},
},
exclude: /node_modules/,
},
],
},
};
webpack(config, (err, stats) => {
t.is(err, null);
t.is(stats.compilation.warnings.length, 0);
t.is(stats.compilation.errors.length, 0);
t.end();
});
},
);

test.cb("should return compilation error about a *.vue file", t => {
const config = {
mode: "development",
entry: {
firstEntry: path.join(__dirname, "fixtures/basic.js"),
secondEntry: path.join(__dirname, "fixtures/TestTS.ts"),
thirdEntry: path.join(__dirname, "fixtures/TestTsVUE.vue"),
},
output: {
path: t.context.directory,
},
module: {
rules: [
{
test: /\.((jsx?)|ts|vue)$/,
loader: babelLoader,
options: {
presets: ["@babel/preset-env", "@babel/preset-typescript"],
},
exclude: /node_modules/,
},
],
},
};
webpack(config, (err, stats) => {
t.is(err, null);
t.is(stats.compilation.warnings.length, 0);
t.is(stats.compilation.errors.length, 1);
const moduleBuildError = stats.compilation.errors[0];
const babelLoaderError = moduleBuildError.error;
t.regex(
babelLoaderError.stack,
/SyntaxError.+TestTsVUE.vue.+Unexpected token/,
);
t.end();
});
});
23 changes: 23 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,13 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-syntax-typescript@^7.0.0":
version "7.1.5"
resolved "https://registry.yarnpkg.com/@babel/plugin-syntax-typescript/-/plugin-syntax-typescript-7.1.5.tgz#956a1f43dec8a9d6b36221f5c865335555fdcb98"
integrity sha512-VqK5DFcS6/T8mT5CcJv1BwZLYFxkHiGZmP7Hs87F53lSToE/qfL7TpPrqFSaKyZi9w7Z/b/tmOGZZDupcJjFvw==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-transform-arrow-functions@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-arrow-functions/-/plugin-transform-arrow-functions-7.0.0.tgz#a6c14875848c68a3b4b3163a486535ef25c7e749"
Expand Down Expand Up @@ -524,6 +531,14 @@
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"

"@babel/plugin-transform-typescript@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-typescript/-/plugin-transform-typescript-7.1.0.tgz#81e7b4be90e7317cbd04bf1163ebf06b2adee60b"
integrity sha512-TOTtVeT+fekAesiCHnPz+PSkYSdOSLyLn42DI45nxg6iCdlQY6LIj/tYqpMB0y+YicoTUiYiXqF8rG6SKfhw6Q==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-syntax-typescript" "^7.0.0"

"@babel/plugin-transform-unicode-regex@^7.0.0":
version "7.0.0"
resolved "https://registry.yarnpkg.com/@babel/plugin-transform-unicode-regex/-/plugin-transform-unicode-regex-7.0.0.tgz#c6780e5b1863a76fe792d90eded9fcd5b51d68fc"
Expand Down Expand Up @@ -578,6 +593,14 @@
js-levenshtein "^1.1.3"
semver "^5.3.0"

"@babel/preset-typescript@^7.1.0":
version "7.1.0"
resolved "https://registry.yarnpkg.com/@babel/preset-typescript/-/preset-typescript-7.1.0.tgz#49ad6e2084ff0bfb5f1f7fb3b5e76c434d442c7f"
integrity sha512-LYveByuF9AOM8WrsNne5+N79k1YxjNB6gmpCQsnuSBAcV8QUeB+ZUxQzL7Rz7HksPbahymKkq2qBR+o36ggFZA==
dependencies:
"@babel/helper-plugin-utils" "^7.0.0"
"@babel/plugin-transform-typescript" "^7.1.0"

"@babel/[email protected]":
version "7.0.0-beta.40"
resolved "https://registry.yarnpkg.com/@babel/template/-/template-7.0.0-beta.40.tgz#034988c6424eb5c3268fe6a608626de1f4410fc8"
Expand Down