weProvide / aviate-magento2
此包已弃用且不再维护。未建议替代包。
Magento 2 的 Aviate 实现
1.0.0
2018-02-23 15:19 UTC
Requires
- weprovide/aviate: ^0.0.2
This package is not auto-updated.
Last update: 2024-05-28 09:30:21 UTC
README
Magento 2 的 Aviate 实现。
安装
composer require weprovide/aviate-magento2
配置
const path = require('path') // NAMESPACE/THEME Needs to match your exact theme namespace and name. Since the module will automatically load NAMESPACE/THEME.css module.exports = { // For production we expect the assets to be in web/dist distLocations: [ path.join(__dirname, 'app/design/frontend/NAMESPACE/THEME/web/dist') ], decorateConfig(config) { config.entry = Object.assign({}, config.entry, { 'NAMESPACE/THEME': [ path.join(__dirname, 'app/design/frontend/NAMESPACE/THEME/styles/styles.scss') ] }) // Allows you to use relative paths to theme in your CSS and Javascript. For SASS you can use @import "~theme/path/to/file" config.resolve = { alias: { theme: path.join(__dirname, 'app/design/frontend/NAMESPACE/THEME') } } return config } }
添加自定义文件
在某些情况下,您可能需要添加自定义文件,例如 React 应用程序。您可以使用 Magento 2 拦截器 来实现此功能。
aviate.config.js
(在项目根目录中)
const path = require('path') // NAMESPACE/THEME Needs to match your exact theme namespace and name. Since the module will automatically load NAMESPACE/THEME.css module.exports = { // For production we expect the assets to be in web/dist distLocations: [ path.join(__dirname, 'app/design/frontend/NAMESPACE/THEME/web/dist') ], decorateConfig(config) { config.entry = Object.assign({}, config.entry, { 'NAMESPACE/THEME': [ path.join(__dirname, 'app/design/frontend/NAMESPACE/THEME/styles/styles.scss') ], // This part is the custom file 'NAMESPACE/THEME/react': [ 'react-hot-loader/patch', path.join(__dirname, 'app/design/frontend/NAMESPACE/THEME/react/app.js'), ] }) // Allows you to use relative paths to theme in your CSS and Javascript. For SASS you can use @import "~theme/path/to/file" config.resolve = { alias: { theme: path.join(__dirname, 'app/design/frontend/NAMESPACE/THEME') } } return config } }
etc/di.xml
:
<?xml version="1.0" ?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="WeProvide\Aviate\Magento2\Block\DevServer"> <plugin disabled="false" name="Add_React" sortOrder="10" type="NAMESPACE\MODULE\Plugin\AddReact"/> </type> </config>
Plugin/AddReact.php
:
<?php namespace NAMESPACE\MODULE\Plugin; use WeProvide\Aviate\Magento2\Block\DevServer; class AddReact { public function afterGetFiles( DevServer $subject, $types ) { $aviate = $subject->aviate(); $themePath = $aviate->getTheme()->getThemePath(); if ($aviate->isDevMode()) { $types['js'][] = $aviate->getDevServerUrl($themePath . '/react.js'); return $types; } $types['js'][] = $subject->getViewFileUrl('dist/' . $themePath . '/react.js'); return $types; } }