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

feat: added new url support #753

Merged
merged 3 commits into from Apr 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 9 additions & 8 deletions package-lock.json

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

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -73,7 +73,7 @@
"npm-run-all": "^4.1.5",
"prettier": "^2.2.1",
"standard-version": "^9.1.0",
"webpack": "^5.33.2",
"webpack": "^5.36.1",
"webpack-cli": "^4.5.0",
"webpack-dev-server": "^3.7.2"
},
Expand Down
13 changes: 13 additions & 0 deletions src/loader.js
Expand Up @@ -196,6 +196,7 @@ export function pitch(request) {
);
return;
}

this.importModule(
`${this.resourcePath}.webpack[javascript/auto]!=!${request}`,
{
Expand Down Expand Up @@ -230,6 +231,18 @@ export function pitch(request) {
outputOptions
);

// The templates are compiled and executed by NodeJS - similar to server side rendering
// Unfortunately this causes issues as some loaders require an absolute URL to support ES Modules
// The following config enables relative URL support for the child compiler
childCompiler.options.module = { ...childCompiler.options.module };
childCompiler.options.module.parser = {
...childCompiler.options.module.parser,
};
childCompiler.options.module.parser.javascript = {
...childCompiler.options.module.parser.javascript,
url: 'relative',
};

const { NodeTemplatePlugin } = webpack.node;
const NodeTargetPlugin = webpack.node.NodeTargetPlugin
? webpack.node.NodeTargetPlugin
Expand Down
4 changes: 3 additions & 1 deletion test/TestCases.test.js
Expand Up @@ -106,6 +106,7 @@ describe('TestCases', () => {
directoryForCase,
'webpack.config.js'
));
const { context } = webpackConfig;

for (const config of [].concat(webpackConfig)) {
Object.assign(
Expand All @@ -132,7 +133,8 @@ describe('TestCases', () => {
}
return p;
}),
}
},
context ? { context } : {}
);
}

Expand Down
Expand Up @@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
/******/
/******/ /* webpack/runtime/getFullHash */
/******/ (() => {
/******/ __webpack_require__.h = () => ("acd0bc1ae24d05fdac73")
/******/ __webpack_require__.h = () => ("619e4b98882ea3a2aba3")
/******/ })();
/******/
/******/ /* webpack/runtime/global */
Expand Down
Expand Up @@ -73,7 +73,7 @@ __webpack_require__.r(__webpack_exports__);
/******/
/******/ /* webpack/runtime/getFullHash */
/******/ (() => {
/******/ __webpack_require__.h = () => ("4ecab8e2ff0fe228a728")
/******/ __webpack_require__.h = () => ("962555dd7355e6c261df")
/******/ })();
/******/
/******/ /* webpack/runtime/global */
Expand Down
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/cases/new-url-with-public-path-auto/app/index.js
@@ -0,0 +1 @@
import './style.css';
12 changes: 12 additions & 0 deletions test/cases/new-url-with-public-path-auto/app/mockLoader.js
@@ -0,0 +1,12 @@
export default function loader() {
const callback = this.async();

callback(
null,
`export default [
[0, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
[1, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
[2, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
]`
);
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path-auto/app/style.css
@@ -0,0 +1,3 @@
.class {
background: red;
}
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path-auto/expected/main.css
@@ -0,0 +1,3 @@
.foo {background: url(img.png)}
.bar {background: url(../outer-img.png)}
.baz {background: url(nested/nested-img.png)}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/cases/new-url-with-public-path-auto/test.filter.js
@@ -0,0 +1,4 @@
const webpack = require('webpack');

module.exports = () =>
webpack.version[0] !== '4' && !process.env.EXPERIMENTAL_USE_IMPORT_MODULE;
36 changes: 36 additions & 0 deletions test/cases/new-url-with-public-path-auto/webpack.config.js
@@ -0,0 +1,36 @@
import path from 'path';

import Self from '../../../src';

module.exports = {
entry: './index.js',
context: path.resolve(__dirname, 'app'),
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
options: {
publicPath: 'auto',
},
},
'./mockLoader',
],
},
{
test: /\.png$/,
type: 'asset/resource',
generator: {
filename: '[path][name][ext]',
},
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};
Binary file added test/cases/new-url-with-public-path/app/img.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions test/cases/new-url-with-public-path/app/index.js
@@ -0,0 +1 @@
import './style.css';
12 changes: 12 additions & 0 deletions test/cases/new-url-with-public-path/app/mockLoader.js
@@ -0,0 +1,12 @@
export default function loader() {
const callback = this.async();

callback(
null,
`export default [
[0, ".foo {background: url(" + new URL("./img.png", import.meta.url) + ")}", ""],
[1, ".bar {background: url(" + new URL("../outer-img.png", import.meta.url) + ")}", ""],
[2, ".baz {background: url(" + new URL("./nested/nested-img.png", import.meta.url) + ")}", ""]
]`
);
}
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path/app/style.css
@@ -0,0 +1,3 @@
.class {
background: red;
}
3 changes: 3 additions & 0 deletions test/cases/new-url-with-public-path/expected/main.css
@@ -0,0 +1,3 @@
.foo {background: url(public/img.png)}
.bar {background: url(public/../outer-img.png)}
.baz {background: url(public/nested/nested-img.png)}
Binary file added test/cases/new-url-with-public-path/outer-img.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions test/cases/new-url-with-public-path/test.filter.js
@@ -0,0 +1,4 @@
const webpack = require('webpack');

module.exports = () =>
webpack.version[0] !== '4' && !process.env.EXPERIMENTAL_USE_IMPORT_MODULE;
36 changes: 36 additions & 0 deletions test/cases/new-url-with-public-path/webpack.config.js
@@ -0,0 +1,36 @@
import path from 'path';

import Self from '../../../src';

module.exports = {
entry: './index.js',
context: path.resolve(__dirname, 'app'),
module: {
rules: [
{
test: /\.css$/,
use: [
{
loader: Self.loader,
options: {
publicPath: 'public',
},
},
'./mockLoader',
],
},
{
test: /\.png$/,
type: 'asset/resource',
generator: {
filename: '[path][name][ext]',
},
},
],
},
plugins: [
new Self({
filename: '[name].css',
}),
],
};