135 lines
3.6 KiB
JavaScript
135 lines
3.6 KiB
JavaScript
"use strict";
|
|
|
|
Object.defineProperty(exports, "__esModule", {
|
|
value: true
|
|
});
|
|
exports.default = extractInlineAssets;
|
|
function _rust() {
|
|
const data = require("@parcel/rust");
|
|
_rust = function () {
|
|
return data;
|
|
};
|
|
return data;
|
|
}
|
|
function _posthtml() {
|
|
const data = _interopRequireDefault(require("posthtml"));
|
|
_posthtml = function () {
|
|
return data;
|
|
};
|
|
return data;
|
|
}
|
|
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
const SCRIPT_TYPES = {
|
|
'application/ecmascript': 'js',
|
|
'application/javascript': 'js',
|
|
'text/javascript': 'js',
|
|
module: 'js'
|
|
};
|
|
function extractInlineAssets(asset, ast) {
|
|
const program = ast.program;
|
|
let key = 0;
|
|
|
|
// Extract <style> elements for processing.
|
|
const parts = [];
|
|
(0, _posthtml().default)().walk.call(program, node => {
|
|
if (node.tag === 'style' || node.tag === 'script') {
|
|
var _node$location;
|
|
const value = node.content && node.content.join('');
|
|
if (!value) {
|
|
return node;
|
|
}
|
|
let type, env;
|
|
if (node.tag === 'style') {
|
|
if (node.attrs && node.attrs.type != null) {
|
|
type = node.attrs.type.split('/')[1];
|
|
} else {
|
|
type = 'css';
|
|
}
|
|
} else if (node.tag === 'script') {
|
|
if (node.attrs && SCRIPT_TYPES[node.attrs.type]) {
|
|
type = SCRIPT_TYPES[node.attrs.type];
|
|
} else if (node.attrs) {
|
|
type = node.attrs.type.split('/')[1];
|
|
} else {
|
|
type = 'js';
|
|
}
|
|
env = {
|
|
sourceType: node.attrs && node.attrs.type === 'module' ? 'module' : 'script',
|
|
// SVG script elements do not support type="module" natively yet.
|
|
outputFormat: 'global',
|
|
loc: node.location ? {
|
|
filePath: asset.filePath,
|
|
start: node.location.start,
|
|
end: node.location.end
|
|
} : undefined
|
|
};
|
|
}
|
|
if (!type) {
|
|
return node;
|
|
}
|
|
let attrs = node.attrs;
|
|
if (!attrs) {
|
|
attrs = {};
|
|
node.attrs = attrs;
|
|
}
|
|
|
|
// Inform packager to remove type, since CSS and JS are the defaults.
|
|
delete attrs.type;
|
|
let parcelKey;
|
|
// allow a script/style tag to declare its key
|
|
if (attrs['data-parcel-key']) {
|
|
parcelKey = attrs['data-parcel-key'];
|
|
} else {
|
|
parcelKey = (0, _rust().hashString)(`${asset.id}:${key++}`);
|
|
}
|
|
|
|
// insert parcelId to allow us to retrieve node during packaging
|
|
attrs['data-parcel-key'] = parcelKey;
|
|
asset.setAST(ast); // mark dirty
|
|
|
|
asset.addDependency({
|
|
specifier: parcelKey,
|
|
specifierType: 'esm'
|
|
});
|
|
parts.push({
|
|
type,
|
|
content: value,
|
|
uniqueKey: parcelKey,
|
|
bundleBehavior: 'inline',
|
|
env,
|
|
meta: {
|
|
type: 'tag',
|
|
// $FlowFixMe
|
|
node,
|
|
startLine: (_node$location = node.location) === null || _node$location === void 0 ? void 0 : _node$location.start.line
|
|
}
|
|
});
|
|
}
|
|
|
|
// Process inline style attributes.
|
|
let attrs = node.attrs;
|
|
let style = attrs === null || attrs === void 0 ? void 0 : attrs.style;
|
|
if (attrs != null && style != null) {
|
|
let parcelKey = (0, _rust().hashString)(`${asset.id}:${key++}`);
|
|
attrs.style = asset.addDependency({
|
|
specifier: parcelKey,
|
|
specifierType: 'esm'
|
|
});
|
|
asset.setAST(ast); // mark dirty
|
|
|
|
parts.push({
|
|
type: 'css',
|
|
content: style,
|
|
uniqueKey: parcelKey,
|
|
bundleBehavior: 'inline',
|
|
meta: {
|
|
type: 'attr',
|
|
// $FlowFixMe
|
|
node
|
|
}
|
|
});
|
|
}
|
|
return node;
|
|
});
|
|
return parts;
|
|
} |